/*
 ============================================================================
 Name        : TP2.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BYTES_DEL_DIGEST 33
#define MAX_C 63

const char* caracteres = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"};

void findPass1(char* caract,char* buffer_leido,char* buffer){
	int i;
	for (i = 0; i < MAX_C; i++) {
		caract[0] = caracteres[i];
		get_sum(caract, buffer);
		if (strcmp(buffer_leido, buffer) == 0) {
			printf("%s\n", caract);
			break;
		}
	}
}

void findPass2(char* caract,char* buffer_leido,char* buffer){
	int i,j;
	for (i = 0; i < MAX_C; i++) {
		caract[0] = caracteres[i];
		for(j = 0; j < MAX_C; j++) {
			caract[1] = caracteres[j];
			get_sum(caract, buffer);
			if (strcmp(buffer_leido, buffer) == 0) {
				printf("%s\n", caract);
				break;
			}
		}
	}
}

void findPass3(char* caract,char* buffer_leido,char* buffer){
	int i,j,k;
	for (i = 0; i < MAX_C; i++) {
		caract[0] = caracteres[i];
		for(j = 0; j < MAX_C; j++) {
			caract[1] = caracteres[j];
			for(k = 0; k < MAX_C; k++) {
				caract[2] = caracteres[k];
				get_sum(caract, buffer);
				if (strcmp(buffer_leido, buffer) == 0) {
					printf("%s\n", caract);
					break;
				}
			}
		}
	}
}


int main(int argc, char **argv) { //arg1 es el archivo
								  //arg2 cant de bytes
	char * path = argv[1];
	char * cant_bytes_c = argv[2]; //en el password
	int cant_bytes = atoi(cant_bytes_c);
	if (cant_bytes > 3) return 1; //todo mal
	FILE * file = fopen(path, "r");

	char c[cant_bytes + 1];
	c[cant_bytes] = 0;
	char *buffer = (char *) malloc(BYTES_DEL_DIGEST);
	char *buffer_leido = (char *) malloc(BYTES_DEL_DIGEST);
	memset(buffer, 0, BYTES_DEL_DIGEST);//DEBUG, hay que sacarlo
	memset(buffer_leido, 0, BYTES_DEL_DIGEST);//DEBUG, hay que sacarlo

	while (fgets(buffer_leido, BYTES_DEL_DIGEST + 1, file)){ // el +1 es por \n
		strtok(buffer_leido,"\n"); //Remuevo el \n
		if(cant_bytes==1){
			findPass1(c, buffer_leido, buffer);
		}
		else if(cant_bytes == 2){
			findPass2(c,buffer_leido, buffer);
		}
		else if(cant_bytes == 3){
			findPass3(c,buffer_leido, buffer);

		}

		/*for (c[0] = -128; c[0] < 127; c[0]++) {
			if (cant_bytes > 1) {
				for (c[1] = -128; c[1] < 127; c[1]++) {
					if (cant_bytes > 2) {
						for (c[2] = -128; c[2] < 127; c[2]++) {
							get_sum(c, buffer);
							if (strcmp(buffer_leido, buffer) == 0) {
								printf("%s\n", c);
								break;
							}
						}
					}else{
						get_sum(c, buffer);
						if (strcmp(buffer_leido, buffer) == 0) {
							printf("%s\n", c);
							break;
						}
					}
				}
			}else{
				get_sum(c, buffer);
				if (strcmp(buffer_leido, buffer) == 0) {
					printf("%s\n", c);
					break;
				}
			}
		}
	}*/
	}
	free(buffer);
	free(buffer_leido);
	return EXIT_SUCCESS;
}
